package git;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;

import java.util.List;

/**
 * Gitee SDK
 * https://gitee.com/api/v5/swagger#/getV5UserRepos
 */
public class GiteeSdk {
    private final DataBind dataBind;

    public GiteeSdk(DataBind dataBind) {
        this.dataBind = dataBind;
    }

    // 所有仓库
    public List<Resp> getRepos() {
        String s = HttpUtil.get("https://gitee.com/api/v5/user/repos?access_token="+dataBind.getPassword()+"&visibility=all&affiliation=owner&sort=created&direction=desc&page=1&per_page=100");
        List<Resp> resps = JSONUtil.parseArray(s).toList(Resp.class);
        resps.forEach(repo->{
            String[] split = repo.getFull_name().split("/");
            repo.setOwner(split[0]);
            repo.setRepo(split[1]);
        });
        return resps;
    }

    // 删除仓库
    public String deleteRepo(String owner, String repo) {
        String url = StrUtil.format("https://gitee.com/api/v5/repos/{}/{}?access_token="+dataBind.getPassword(), owner, repo);
        HttpResponse execute = HttpRequest.delete(url).execute();
        return execute.body();
    }
}
